package ch.gibb.learnit.client.controllers;

import ch.gibb.learnit.client.util.soap.Category;
import ch.gibb.learnit.client.util.soap.User;
import ch.gibb.learnit.client.util.soap.Word;
import org.apache.log4j.Logger;

import java.util.List;


/**
 * Controller to record a new word
 *
 * @author Bernard Jaquet
 * @version 1.0
 */
public class RecordAction extends MasterAction {
    //IN
    private String question;
    private String response;
    private String categoryId;
    private String twice;
    private String record;

    //OUT
    private List<Category> userCategorys;

    //Class
    Logger masterLogger = Logger.getLogger(RecordAction.class);


    @Override
    public int openPage() {
        // Setting title
        super.title = "Lernkarten erfassen";

        // Checking if a user is logged in
        if(!loginCheck())
            return 403;

        // Getting categories
        try {
            User user = getUser();
            if(user == null){
                getErrors().addMessage("Wegen eines interen Fehlers konnten die Benutzerdaten nicht geladen werden");
                return 0;
            }
            userCategorys = client.getCategories(user);
            if(userCategorys == null){

            }

        } catch (Exception e) {
            getErrors().addMessage("Die Kategorien konnten wegen eines internen Fehlers nicht ausgelesen werden");
            masterLogger.error("Could not get Categories because " + e.getMessage(), e);
        }

        // Logic for record a word
        if (record != null && !record.equals("")) {
            try {
                boolean success = true;
                success = (success & registerWord(question, response));
                if(twice != null && !twice.equals(""))
                    success = (success & registerWord(response, question));
                if (success) {
                    getSuccess().addMessage("Die Karte wurde erfolgreich erfasst.");
                } else {
                    masterLogger.warn("Could not register word becaouse of an unknown error");
                    getErrors().addMessage("Die Karte konnte wegen eined unbekannten fehlers nicht erfasst werden.");
                }
            } catch (Exception e) {
                getErrors().addMessage("Es ist ein unbekannter Fehler aufgetreten.");
                masterLogger.error("Could not register word because " + e.getMessage(), e);
            }
        }
        return 0;
    }

    /**
     * Creates a new Word
     *
     * @param quest
     * @param answ
     * @return
     * @throws Exception
     */
    private boolean registerWord(String quest, String answ) throws Exception {
        Word word = factory.createWord();
        word.setQ(factory.createWordQ(quest));
        word.setA(factory.createWordA(answ));
        word.setActive(true);
        User user = getUser();
        masterLogger.debug("Recording word " + word);
        return client.addWord(user, client.getCategory(user, Integer.valueOf(categoryId)), word);
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public String getRecord() {
        return record;
    }

    public void setRecord(String record) {
        this.record = record;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String getTwice() {
        return twice;
    }

    public void setTwice(String twice) {
        this.twice = twice;
    }

    public List<Category> getUserCategorys() {
        return userCategorys;
    }

    public void setUserCategorys(List<Category> userCategorys) {
        this.userCategorys = userCategorys;
    }
}
